package com.jy.modules.cims.common.open.api.crypt;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.jy.modules.cims.common.open.api.util.JieYueApiUtils;

public class CryptHelper {
	public static String aesEncrypt(String msg, String key) {
		AESCrypt aes = new AESCrypt(key);
		return aes.encrypt(msg);
	}

	public static String aesEncrypt(Charset charset, String msg, String key) {
		AESCrypt aes = new AESCrypt(key, charset);
		return aes.encrypt(msg);
	}

	public static String aesDecrypt(String msg, String key) {
		AESCrypt aes = new AESCrypt(key);
		return aes.decrypt(msg);
	}

	public static String aesDecrypt(Charset charset, String msg, String key) {
		AESCrypt aes = new AESCrypt(key, charset);
		return aes.decrypt(msg);
	}

	public static String md5(Object obj, String md5Key, String... exclude) {
		if (obj == null) {
			return null;
		}
		Map<String, String> param = JieYueApiUtils.obj2Map(obj, exclude);
		return md5(param, md5Key);
	}

	public static String md5(Map<String, String> param, String md5Key) {
		if ((param == null) || (param.size() == 0)) {
			return null;
		}
		List<Map.Entry<String, String>> list = sort(param);

		String msg = assemble(list);

		String message = msg + md5Key;

		MD5Crypt m = new MD5Crypt();
		return m.md5(message);
	}

	public static String md5(Charset charset, Object obj, String md5Key, String... exclude) {
		if (obj == null) {
			return null;
		}
		Map<String, String> param = JieYueApiUtils.obj2Map(obj, exclude);
		return md5(charset, param, md5Key);
	}

	public static String md5(Charset charset, Map<String, String> param, String md5Key) {
		if ((param == null) || (param.size() == 0)) {
			return null;
		}
		List<Map.Entry<String, String>> list = sort(param);

		String msg = assemble(list);

		String message = msg + md5Key;

		MD5Crypt m = new MD5Crypt(charset);
		return m.md5(message);
	}

	private static String assemble(List<Map.Entry<String, String>> list) {
		String msg = null;

		StringBuilder sb = new StringBuilder();

		int i = 0;
		for (int j = list.size(); i < j; i++) {
			Map.Entry<String, String> entry = (Entry<String, String>) list.get(i);
			sb.append((String) entry.getKey()).append((String) entry.getValue());
		}
		msg = sb.toString();
		return msg;
	}

	@SuppressWarnings("unchecked")
	private static List<Map.Entry<String, String>> sort(Map<String, String> param) {
		Set<Map.Entry<String, String>> set = param.entrySet();
		List<Map.Entry<String, String>> list = new ArrayList<Entry<String, String>>(set);

		Collections.sort(list, new Comparator<Object>() {
			public int compare(Object o1, Object o2) {
				if (o1 instanceof Map.Entry && o2 instanceof Map.Entry) {
					return ((String) ((Map.Entry<String, String>) o1).getKey()).compareTo((String) ((Map.Entry<String, String>) o2).getKey());
				}
				return 0;
			}
		});
		return list;
	}
}
